﻿Public Class frmPersona
    Private frmRPersona As New frmMPersona
    Public arrConsulta(15) As String
    Private arrCampos() As String = {
        "persona_id",
        "tipodocumento_id",
        "alias_id",
        "ciudad_id",
        "sexo_id ",
        "tiposangre_id",
        "persona_cedula",
        "persona_pnombre",
        "persona_snombre",
        "persona_papellido",
        "persona_sapellido",
        "persona_ptelefono",
        "persona_stelefono",
        "persona_correo",
        "persona_fax",
        "persona_fechanacimiento"}

    Private Sub frmPersona_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        refreshList()

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("tipodocumento")
        mdlCRUD.loadRecord(dstRecord, "tipodocumento", "tipodocumento", "", Nothing)
        frmRPersona.frmParent = Me

        frmRPersona.cmbTipodocumento.DataSource = dstRecord.Tables("tipodocumento")
        frmRPersona.cmbTipodocumento.DisplayMember = dstRecord.Tables("tipodocumento").Columns(1).Caption.ToString
        frmRPersona.cmbTipodocumento.ValueMember = dstRecord.Tables("tipodocumento").Columns(0).Caption.ToString

        dstRecord.Tables.Add("ciudad")
        mdlCRUD.loadRecord(dstRecord, "ciudad", "ciudad", "", Nothing)

        frmRPersona.cmbCiudad.DataSource = dstRecord.Tables("ciudad")
        frmRPersona.cmbCiudad.DisplayMember = dstRecord.Tables("ciudad").Columns(2).Caption.ToString
        frmRPersona.cmbCiudad.ValueMember = dstRecord.Tables("ciudad").Columns(0).Caption.ToString

        dstRecord.Tables.Add("sexo")
        mdlCRUD.loadRecord(dstRecord, "sexo", "sexo", "", Nothing)

        frmRPersona.cmbSexo.DataSource = dstRecord.Tables("sexo")
        frmRPersona.cmbSexo.DisplayMember = dstRecord.Tables("sexo").Columns(1).Caption.ToString
        frmRPersona.cmbSexo.ValueMember = dstRecord.Tables("sexo").Columns(0).Caption.ToString

        dstRecord.Tables.Add("rh")
        mdlCRUD.loadRecord(dstRecord, "rh", "rh", "", Nothing)

        frmRPersona.cmbrh.DataSource = dstRecord.Tables("rh")
        frmRPersona.cmbrh.DisplayMember = dstRecord.Tables("rh").Columns(1).Caption.ToString
        frmRPersona.cmbrh.ValueMember = dstRecord.Tables("rh").Columns(0).Caption.ToString

        dstRecord.Tables.Add("tiposangre")
        mdlCRUD.loadRecord(dstRecord, "tiposangre", "tiposangre", "", Nothing)

        frmRPersona.cmbTiposangre.DataSource = dstRecord.Tables("tiposangre")
        frmRPersona.cmbTiposangre.DisplayMember = dstRecord.Tables("tiposangre").Columns(1).Caption.ToString
        frmRPersona.cmbTiposangre.ValueMember = dstRecord.Tables("tiposangre").Columns(0).Caption.ToString
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("persona")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= If(strCondicion <> "", " AND ", "") & If(i = 2, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlCRUD.loadRecord(dstRecord, "persona", "persona", strCondicion, Nothing)

        Me.dgvPersona.DataSource = dstRecord.Tables("persona")
        Me.dgvPersona.Columns(0).HeaderText = "Cedula"
        Me.dgvPersona.Columns(1).HeaderText = "Nombre"
        Me.dgvPersona.Columns(2).HeaderText = "Apellido"
        'Me.dgvTipoelemento.Columns(3).HeaderText = "tipoelemento"
        Me.dgvPersona.Columns(0).Width = 60
        Me.dgvPersona.Columns(1).Width = 260
        Me.dgvPersona.Columns(2).Width = 260
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvPersona.Rows(Me.dgvPersona.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            frmRPersona.modeForm = 3
            frmRPersona.Text = "Editar Persona"
            frmRPersona.ShowDialog()
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Persona")
        End If
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNuevo.Click
        frmRPersona.modeForm = 1
        frmRPersona.Text = "Nuevo Persona"
        frmRPersona.ShowDialog()
    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbRefresh.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()

    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbConsultar.Click
        frmRPersona.modeForm = 2
        frmRPersona.Text = "Consultar Persona"
        frmRPersona.ShowDialog()
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar Persona")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvPersona.Rows(Me.dgvPersona.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("persona", "persona_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub
End Class